#include "esp_common.h"
#include "user_config.h"
#include "espressif/spi_flash.h"

SpiFlashOpResult get_mode_info(get_mode_inf_t *mode_info)
{
	et_int32 rc=SPI_FLASH_RESULT_OK;
	
	rc = spi_flash_read(APPKEY_UID_FASH_SECTOR * SPI_FLASH_SEC_SIZE, (et_uint32 *)&mode_info->id, sizeof(id_info_t));
	if(rc != SPI_FLASH_RESULT_OK)
		return rc;
	
	strcpy(mode_info->hard_version, HARDWARE_VERSION);
	strcpy(mode_info->soft_version, SOFTWARE_VERSION);
	strcpy(mode_info->sdk_version, system_get_sdk_version());

	return rc;
}

SpiFlashOpResult set_mode_info(id_info_t * id_info)
{
	et_int32 rc=SPI_FLASH_RESULT_OK;
	
	rc = spi_flash_erase_sector(APPKEY_UID_FASH_SECTOR);
	if(rc != SPI_FLASH_RESULT_OK)
		return rc;
	
	rc = spi_flash_write(APPKEY_UID_FASH_SECTOR * SPI_FLASH_SEC_SIZE, (et_uint32 *)id_info, sizeof(id_info_t));
	if(rc != SPI_FLASH_RESULT_OK)
		return rc;

	return rc;
}

et_int32 get_wifi_station_info(wifi_info_t *buf)
{
	sta_cfg_t sta_cfg;
	
	if (NULL == buf)
	{
		printf("parameter buf is NULL!!!\n");
		return RETURN_ERR;
	}
	
	if (!wifi_get_ip_info(STATION_IF, &buf->ip_info))
	{
		printf("get station ip info fail\n");
		return RETURN_ERR;
	}

	if (DHCP_STARTED == wifi_station_dhcpc_status())
	{
		buf->ip_mode = DHCP_MODE;
	}
	else
	{
		buf->ip_mode = STATIC_MODE;
	}

	if (!wifi_get_macaddr(STATION_IF, buf->mac))
	{
		printf("get station mac info fail\n");
		return RETURN_ERR;
	}

	if (!wifi_station_get_config(&sta_cfg))
	{
		printf("get station ssid info fail\n");
		return RETURN_ERR;
	}
	strncpy(buf->ssid, sta_cfg.ssid, SSID_LEN);
	
	return RETURN_OK;
}